// index.js
// 获取应用实例

let network = require("../../utils/network.js")

let app = getApp();
let url  = app.globalData.url;
Page({
  data: {
    background:[],
    goods:[],
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    currentSwiper:0,
    discount:0,
  },
  // 事件处理函数
  onLoad() {
    var that = this
    let times = setInterval(function(){
      if(wx.getStorageSync('discount')){
        that.setData({
          discount: wx.getStorageSync('discount')
        })
        wx.setTabBarStyle({
          color: '#BBBBBD',
          selectedColor: '#FF496C'
        }),
          network.request({
          url: url + '/AppletsApi/Index/index.html',
          header: {
            "content-type": "application/x-www-form-urlencoded"
          },
          success: function (res) {
            that.setData({
              background: res.data.data.banner,
              goods: res.data.data.goods
            })
          }
        })
        clearInterval(times)
        return false
      }
    }, 100)
  },

  // banner对应的点
  swiperChange: function (e) {
    this.setData({
      currentSwiper: e.detail.current
    })
  },
  // 添加商品
  shop:function(e){
    let path = e.currentTarget.dataset.path
    console.log(path)
    wx.navigateTo({
      url: path,
    })
  },
  cart: function (e) {
    var that = this
    var id = e.currentTarget.dataset.id
    network.request({
      url: url + '/AppletsApi/Goods/addToCart.html',
      data: {
        applet_openid: wx.getStorageSync('applet_openid'),
        goods_id: id,
        goods_num: 1,
      },
      method: 'post',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        console.log(res)
        wx.reLaunch({
          url: '/pages/cart/cart'
        })
      }
    })


  }



})
